package com.offcn.scwproject.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.scwproject.pojo.*;
import com.offcn.scwproject.service.ProjectInfoService;
import com.offcn.scwproject.vo.resp.ProjectDetailVo;
import com.offcn.scwproject.vo.resp.ProjectVo;
import com.offcn.utils.OssTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
@Api(tags="项目模块--包含图片上传")
@Slf4j
public class ProjectInformController {
    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private OssTemplate ossTemplate;
    //文件上传
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public AppResponse<Map<String,Object>> upload(@RequestParam("files") MultipartFile[] files)throws Exception{
        Map map=new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        if (files!=null&&files.length>0){
            for(MultipartFile file :files){
                if(!file.isEmpty()){
                    String upload = ossTemplate.upload(file.getInputStream(), file.getOriginalFilename());
                   list.add(upload);
                }
            }
        }
map.put("urls",list);
log.debug("urls的地址：{}===集合是：",list,ossTemplate);
  return AppResponse.ok(map);
    }
    //
    @GetMapping("/details/returns/{projectId}")
    public  AppResponse<List<TReturn>>  getReturnList(@PathVariable("projectId") Integer projectId){
       List<TReturn> returnList= projectInfoService.getReturnList(projectId);
       return AppResponse.ok(returnList);
    }
    //查询所有项目
    @ApiOperation("查询所有项目")
    @GetMapping("/all")
public AppResponse<List<ProjectVo>> findAllProject(){
        //放项目的集合
        List<ProjectVo> list=new ArrayList<>();
        List<TProject> projectList = projectInfoService.findProjectList();
        //遍历
        for(TProject tProject :projectList){
            ProjectVo projectVo = new ProjectVo();
            BeanUtils.copyProperties(tProject,projectVo);
            //获取项目图片

            List<TProjectImages> projectImageList = projectInfoService.findProjectImageList(tProject.getId());
            for(TProjectImages tProjectImages: projectImageList){
                if (tProjectImages.getImgtype()==0){
                    //设置头图
                    projectVo.setHeaderImage(tProjectImages.getImgurl());

                }
            }
            list.add(projectVo);
        }
        return AppResponse.ok(list);
    }
    //获取项目的详情
    @ApiOperation("获取项目信息详情")
    @GetMapping("/findProjectInfo/{projectId}")
 public AppResponse<ProjectDetailVo> findProjectInfo(@PathVariable("projectId")Integer projectId){
        //查询项目
        TProject projectByProjectId = projectInfoService.findProjectByProjectId(projectId);
        ProjectDetailVo projectDetailVo = new ProjectDetailVo();
        BeanUtils.copyProperties(projectByProjectId,projectDetailVo);
        //设置详细图片
        List<TProjectImages> projectImageList = projectInfoService.findProjectImageList(projectId);
        ArrayList<String> strings = new ArrayList<>();
        for (TProjectImages tProjectImages : projectImageList) {
            if(tProjectImages.getImgtype()==0){
                projectDetailVo.setHeaderImage(tProjectImages.getImgurl());
            }else {
                strings.add(tProjectImages.getImgurl());
            }
        }
 //设置详图
        projectDetailVo.setDetailsImage(strings);
        //设置目录
        List<TReturn> returnList = projectInfoService.getReturnList(projectId);
        projectDetailVo.setProjectReturns(returnList);
        return AppResponse.ok(projectDetailVo);
    }
    @ApiOperation("获取系统所有的项目标签")
    @GetMapping("/findAllTag")
    public AppResponse<List<TTag>> findAllTag(){
        List<TTag> projectTagList = projectInfoService.findProjectTagList();
        return AppResponse.ok(projectTagList);
    }
    @ApiOperation("获取系统所有的项目分类")
    @GetMapping("/findAllType")
    public AppResponse<List<TType>> findAllType(){
        List<TType> projectTypeList = projectInfoService.findProjectTypeList();
        return AppResponse.ok(projectTypeList);
    }
    @ApiOperation("获取回报信息")
    @GetMapping("/returns/info/{returnId}")
    //获取回报id
    public AppResponse<TReturn> findReturn(@PathVariable("returnId") Integer returnId){
        TReturn projectReturn = projectInfoService.findProjectReturn(returnId);
        return AppResponse.ok(projectReturn);
    }
}
